package com.rsxy.service.controller;

import com.alibaba.fastjson.JSONObject;
import com.rsxy.service.pojo.RsUser;
import com.rsxy.service.tools.RedisUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
@Api(tags = "视图接口")
public class ViewController {
  @Autowired
  private RedisUtil redisUtil;
    @RequestMapping(method = RequestMethod.GET,value={"/", "login","login.html"})
    public String index(){
        return "login";
    }
    @RequestMapping(method = RequestMethod.GET,value={"/admin", "admin/index","admin/index.html","index","index.html"})
   public ModelAndView test(HttpServletRequest req,String phone) {
        // UserEntity userEntity = getCurrentUser(req);
        //redisUtil.get("")
       // log.info(phone);
        ModelAndView mv = new ModelAndView();

        //  String phone = req.getParameter("phone");
        if (redisUtil.hasKey(phone)){
            String o =(String) redisUtil.get(phone);
            //JSONObject.toJavaObject(o)
            // Object parse = JSONObject.parse(o);
            JSONObject jsonObject = JSONObject.parseObject(o);
            RsUser rsUser = JSONObject.toJavaObject(jsonObject, RsUser.class);
            mv.addObject("user", rsUser);
            mv.setViewName("/admin/index");
        }else {
            mv.setViewName("redirect:/login");
        }
        return mv;
    }
    @RequestMapping(method = RequestMethod.GET,value = "page/{index}/{index1}/{index2}")
    public String page(@PathVariable("index")  String index,@PathVariable("index1")  String index1,@PathVariable("index2")  String index2){

        return "page/"+index+"/"+index1+"/"+index2;

    }
    @RequestMapping(method = RequestMethod.GET,value = "/loginout")
    public ModelAndView loginout(String phone){
        if (redisUtil.hasKey(phone)){
            redisUtil.del(phone);
        }
        ModelAndView mv=new ModelAndView();
        mv.setViewName("redirect:/login");
        return mv;
    }
    @RequestMapping(method = RequestMethod.GET,value = "page/{index}/{index1}")
    public String page1(@PathVariable("index")  String index,@PathVariable("index1")  String index1){
        return "page/"+index+"/"+index1;
    }
}
